package model

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ PmsSkuInfoModel = (*customPmsSkuInfoModel)(nil)

type (
	// PmsSkuInfoModel is an interface to be customized, add more methods here,
	// and implement the added methods in customPmsSkuInfoModel.
	PmsSkuInfoModel interface {
		pmsSkuInfoModel
		FindSaleAttrsBySpuId(ctx context.Context, spuId int64) ([]SaleAttrResult, error)
	}

	customPmsSkuInfoModel struct {
		*defaultPmsSkuInfoModel
	}
)

func (c *customPmsSkuInfoModel) FindSaleAttrsBySpuId(ctx context.Context, spuId int64) ([]SaleAttrResult, error) {
	query := fmt.Sprintf("SELECT pssav.attr_id,pssav.attr_name,GROUP_CONCAT(DISTINCT pssav.attr_value) attr_values " +
		"FROM pms_sku_info info INNER JOIN pms_sku_sale_attr_value pssav on info.sku_id = pssav.sku_id WHERE info.spu_id = ? GROUP BY pssav.attr_id, pssav.attr_name")
	var resp []SaleAttrResult
	err := c.conn.QueryRowsCtx(ctx, &resp, query, spuId)
	return resp, err
}

// NewPmsSkuInfoModel returns a model for the database table.
func NewPmsSkuInfoModel(conn sqlx.SqlConn) PmsSkuInfoModel {
	return &customPmsSkuInfoModel{
		defaultPmsSkuInfoModel: newPmsSkuInfoModel(conn),
	}
}

type SaleAttrResult struct {
	AttrId    int64  `db:"attr_id"`
	AttrName  string `db:"attr_name"`
	AttrValue string `db:"attr_values"`
}
